package cn.bdqn.web.realm;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import cn.bdqn.dao.devuser.DevUserDao;
import cn.bdqn.entity.DevUser;

public class CustomRealm  extends AuthorizingRealm{

	
	
	@Autowired
	private DevUserDao devUserDao;
	
	
	
	@Override
	public String getName() {
		return "CustomRealm";
	}
	
	
		
	
	
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		
		System.out.println("开始认证.....");
		
		//获得用户名
		String username = (String) token.getPrincipal();
		
		//拿着用户名去数据库查询
		DevUser devUser = devUserDao.checkDevUserByUserName(username);
		
		if(devUser == null){
			return null;
		}
		
		Session session = SecurityUtils.getSubject().getSession(); 
		session.setAttribute("devUser", devUser);
		
		return new SimpleAuthenticationInfo(devUser.getDevcode(), devUser.getDevpassword(), getName());
	}

	
	
	
	
	
	
	
	
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(
			PrincipalCollection principals) {
		// TODO Auto-generated method stub
		return null;
	}

	
}

